﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using Codemi.Common.Enums;
using Codemi.Common.Interfaces;
using Codemi.Common.Helpers;
using Codemi.Common.Interfaces.Objects;
using System.Text.RegularExpressions;

namespace Codemi.Common.Managers
{
    public class EmailManager
    {

        private EmailManager(IEmailTemplateProvider emailTemplateProvider)
        {
            this.emailTemplateProvider = emailTemplateProvider;
        }

        private static EmailManager instance;
        public static EmailManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new EmailManager(BootStraper.Initialize<IEmailTemplateProvider>());
                }
                return instance;
            }
        }

        private IEmailTemplateProvider emailTemplateProvider { get; set; }

        public bool SendEmail(MailAddress email, string content)
        {
            throw new NotImplementedException();
        }

        /// <exception>Throws Exception when required parameter not supplied</exception>
        public bool SendEmail(MailAddress email, string templateContent, Dictionary<string, string> dictionaryParameters)
        {
            Regex regex = new Regex(@"{@(\w+?)}", RegexOptions.Multiline);
            templateContent = regex.Replace(templateContent, delegate(Match match)
	            {
                    if (dictionaryParameters.ContainsKey(match.Groups[1].Value))
                    {
                        return dictionaryParameters[match.Groups[1].Value];
                    }
                    else
                    {
                        throw new Exception(string.Format("Parameter {0} was not specified, sending canceled", match.Groups[1].Value));
                    }
	            });
            return SendEmail(email, templateContent);
        }

        public bool SendEmail(MailAddress email, int template, Dictionary<string, string> dictionaryParameters)
        {
            IEmailTemplate templateObj = null;
            templateObj = emailTemplateProvider.GetEmailTemplate(template);
            if (templateObj == null)
                return false;
            else
                return SendEmail(email, templateObj.Content, dictionaryParameters);
        }

        public bool SendEmail(MailAddress email, int? template, EmailTemplates alternativeTemplate, Dictionary<string, string> dictionaryParameters)
        {
            IEmailTemplate templateObj = null;
            if (template.HasValue)
            {
                templateObj = emailTemplateProvider.GetEmailTemplate(template.Value);
            }
            if (templateObj == null)
            {
                templateObj = emailTemplateProvider.GetEmailTemplate(alternativeTemplate);
            }
            return SendEmail(email, templateObj.Content, dictionaryParameters);
        }


    }
}
